<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_DeviceDao extends Vega_Dao_Table {
    /*
     * Hàm lấy các file dành cho máy điện thoại mà
     * hệ thống đã build và test thành công
     */
    public function getClients() {
        $sql = "SELECT * FROM device_client";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function add($data) {
        $device_id = $this->_db->valueQuote($data['device_id']);
        $brand_name = $this->_db->valueQuote($data['brand_name']);
        $model_name = $this->_db->valueQuote($data['model_name']);
        $marketing_name = $this->_db->valueQuote($data['marketing_name']);
        $screen_resolution = $this->_db->valueQuote($data['screen_resolution']);
        $os = $this->_db->valueQuote($data['os']);
        $description = $this->_db->valueQuote($data['description']);
        $client_id = intval($data['client_id']);
        $format = $this->_db->valueQuote($data['format']);
        
        $sql = "INSERT INTO device
                SET device_id=$device_id,
                    brand=$brand_name,
                    model=$model_name,
                    marketing_name=$marketing_name,
                    resolution=$screen_resolution,
                    os=$os,
                    description=$description,
                    client_id=$client_id,
                    format=$format
                ";
        return $this->_db->queryWrite($sql);
    }

    public function getList($page, $limit, $condition) {
        $offset     = $limit * ($page - 1);
        $_limit     = ' LIMIT ' . $limit;
        $_offset    = ' OFFSET ' . $offset;
        $sql = "SELECT device.*, device_client.file_name FROM device LEFT JOIN device_client ON device.client_id = device_client.id
                WHERE $condition
                $_limit $_offset";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function countList($condition) {
        $sql = "SELECT COUNT(id) as num FROM device WHERE $condition ";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();
        return $row['num'];
    }

    public function getDetail($id) {
        $sql = "SELECT device.*, device_client.file_name FROM device LEFT JOIN device_client ON device.client_id = device_client.id WHERE device.id=$id";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }

    public function save($data, $id) {
        $adminId = isset($_SESSION['userid']) ? intval($_SESSION['userid']) : 0;
        $brand_name = $this->_db->valueQuote($data['brand_name']);
        $model_name = $this->_db->valueQuote($data['model_name']);
        $marketing_name = $this->_db->valueQuote($data['marketing_name']);
        $screen_resolution = $this->_db->valueQuote($data['screen_resolution']);
        $os = $this->_db->valueQuote($data['os']);
        $description = $this->_db->valueQuote($data['description']);
        $format = $this->_db->valueQuote($data['format']);
        $vod_format = $this->_db->valueQuote($data['vod_format']);
        $client_id = intval($data['client_id']);
        
        $sql = "UPDATE device
                SET brand=$brand_name,
                    model=$model_name,
                    marketing_name=$marketing_name,
                    resolution=$screen_resolution,
                    os=$os,
                    description=$description,
                    client_id=$client_id,
                    format=$format,
                    vod_format=$vod_format,
                    updated_by=$adminId,
                    updated_time=now()
                WHERE id=$id";

        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list) {
        $list = '(' . implode(',', $list) . ')';
        $sql = "DELETE FROM device WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }

    public function setClientID($list, $client_id, $format, $vod_format) {
        if(!$client_id && !$format && !$vod_format) return false;

        $adminId = isset($_SESSION['userid']) ? intval($_SESSION['userid']) : 0;

        // Where condition
        $_condition = "";
        $_condition = $client_id!=-1    ? $_condition . "client_id=" . intval($client_id) . "," : $_condition;
        $_condition = $format           ? $_condition . "format=" . $this->_db->valueQuote($format) . "," : $_condition;
        $_condition = $vod_format       ? $_condition . "vod_format=" . $this->_db->valueQuote($vod_format) . "," : $_condition;
        
        $list = '(' . implode(',', $list) . ')';
        
        $sql = "UPDATE device SET $_condition updated_by=$adminId, updated_time=now() WHERE id IN $list";
        
        return $this->_db->queryWrite($sql);
    }
}